import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;

import jxl.read.biff.BiffException;


public class NoaaDataWeekend implements INoaaDataWeek{
private IData noaaData;
	
	public NoaaDataWeekend(){
		try {
			this.noaaData = new NoaaData();
			noaaData.setInputFile("c:/CoolingLoadProject/Original Data 20100101-20130101.xls");
			noaaData.read();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @return return the nooaData from Database or Excel
	 */
	public IData getNoaaData(){
		return this.noaaData;
	}
	
	/**
	 * 
	 * @param localTime the local time in database
	 * @param n   n the parameter in ARX(1,2,24)||ARX(24,168);
	 * @return
	 * @throws BiffException
	 * @throws IOException
	 * @throws ParseException
	 */
	
	public IData getLastNHour(String localTime, int n) throws BiffException, IOException, ParseException{
		
		IData lastHourData = new NoaaData();
		
		HashMap<Date, Double> T_t = noaaData.getT_t();
		HashMap<Date, Integer> dayOfWeek = noaaData.getDayofWeek();
		HashMap<Date, Integer> hourOfDay = noaaData.getHour();
		HashMap<Date, Double> T_MAX_t = noaaData.getT_MAX_t();
		HashMap<Date, Double> T_MIN_t = noaaData.getT_MIN_t();
		HashMap<Date, Double> RH_t = noaaData.getRH_t();
		HashMap<Date, Double> CoolLoad_t = noaaData.getCoolLoad_t();
		HashMap<Date, String> time = noaaData.getLocalTime();
		
		
		
		
		Date date = Utility.convertToDate(localTime);
		int currentDay = dayOfWeek.get(date);
//		Utility.debug(currentDay);

		
		
		//Saturday to Monday 
		if(currentDay>= 6 && currentDay<=7){
			date.setHours(date.getHours()-n);
			
			if(date.getDay() == 5){
				date.setHours(date.getHours()- 120);
			}
			
			//System.out.println(date);
			//last hour T_Max_t
			HashMap<Date, Double> lastHour_T_MAX_t = new HashMap<Date, Double>();
			lastHour_T_MAX_t.put(date, T_MAX_t.get(date));	
			lastHourData.setT_MAX_t(lastHour_T_MAX_t);
			lastHourData.setTmaxt(T_MAX_t.get(date));
			
			//last hour T_Min_t
			HashMap<Date, Double> lastHour_T_MIN_t = new HashMap<Date, Double>();
			lastHour_T_MIN_t.put(date, T_MIN_t.get(date));	
			lastHourData.setT_MIN_t(lastHour_T_MIN_t);
			lastHourData.setTmint(T_MIN_t.get(date));
			
			//last hour RH_t
			HashMap<Date, Double> lastHour_RH_t = new HashMap<Date, Double>();
			lastHour_RH_t.put(date, RH_t.get(date));	
			lastHourData.setRH_t(lastHour_RH_t);
			lastHourData.setRHt(RH_t.get(date));
			
			//last hour T_t
			HashMap<Date, Double> lastHour_T_t = new HashMap<Date, Double>();
			lastHour_T_t.put(date, T_t.get(date));
			lastHourData.setT_t(lastHour_T_t);
			lastHourData.setTt(T_t.get(date));
			
			//last hour Cooling Load
			HashMap<Date, Double> lastHour_CoolLoad_t = new HashMap<Date, Double>();
			lastHour_CoolLoad_t.put(date, CoolLoad_t.get(date));
			lastHourData.setCoolLoad_t(lastHour_CoolLoad_t);
			lastHourData.setCoolLoadt(CoolLoad_t.get(date));
			
			//last hour day of week
			HashMap<Date, Integer> lastHour_dayOfWeek = new HashMap<Date, Integer>();
			lastHour_dayOfWeek.put(date, dayOfWeek.get(date));
			lastHourData.setDayofWeek(lastHour_dayOfWeek);
			
			//last hour 
			HashMap<Date, Integer> lastHour_hourOfDay = new HashMap<Date, Integer>();
			lastHour_hourOfDay.put(date, hourOfDay.get(date));
			lastHourData.setHour(lastHour_hourOfDay);
			
			//last hour local time
			HashMap<Date, String> lastHour_localTime = new HashMap<Date, String>();
			lastHour_localTime.put(date, time.get(date));			
			lastHourData.setLocalTime(lastHour_localTime);
		}
		else{
			System.out.println("The day you input is not bewteen Monday and Friday");
		}
		
		return lastHourData;
	}
	
	

	
	public void toString(IData data){
		System.out.println("Local Time"+data.getLocalTime().toString());
		System.out.println("Day of the week"+data.getDayofWeek().toString());
		System.out.println("hour"+ data.getHour().toString());
		System.out.println("Max t"+ data.getT_MAX_t().toString());
		System.out.println("Min t"+ data.getT_MIN_t().toString());
		System.out.println("T_t" + data.getT_t().toString());
		System.out.println("RH_t"+ data.getRH_t().toString());
		System.out.println("Cool Load t"+data.getCoolLoad_t().toString());
		
	}
}
